import 'package:flutter/material.dart';
import 'package:megatron/utils/utils.dart';

import '../const.dart';

class EditText extends StatefulWidget {
  final data;
  final name;
  final labelText;
  final value;
  final icon;
  final keyboardType;

  const EditText(
      {Key key,
      this.data,
      this.name,
      this.labelText,
      this.value,
      this.icon,
      this.keyboardType})
      : super(key: key);
  @override
  _EditTextState createState() => _EditTextState();
}

class _EditTextState extends State<EditText> {
  @override
  Widget build(BuildContext context) {
    return TextField(
      keyboardType: widget.keyboardType,
      controller: TextEditingController.fromValue(TextEditingValue(
          text: widget.value,
          selection: TextSelection.fromPosition(TextPosition(
              affinity: TextAffinity.downstream,
              offset: widget.value.length)))),
      onChanged: (value) {
        widget.data[widget.name] = value;
      },
      style: TextStyle(fontSize: 16, color: Const.black),
      decoration: InputDecoration(
          contentPadding: const EdgeInsets.fromLTRB(16, 10, 0, 10),
          hintText: '请输入用户名',
          hintStyle: TextStyle(color: Const.gray, fontSize: 16),
          filled: true,
          fillColor: Colors.white,
          suffixIcon: GestureDetector(
            onTap: () {
              setState(() {
                widget.data[widget.name] = '';
              });
            },
            child: Image.asset(
              'images/login_btn_delete.png',
              scale: 3,
            ),
          ),
          focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(44),
              ),
              borderSide: BorderSide(
                color: Colors.transparent,
                width: 0,
              )),
          enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(44),
              ),
              borderSide: BorderSide(
                color: Colors.transparent,
                width: 0,
              ))),
    );
  }
}

class PassField extends StatefulWidget {
  final Key fieldKey;
  final String labelText;
  final String helperText;
  final Map map;
  final String mapKey;

  const PassField(
      {Key key,
      this.fieldKey,
      this.labelText,
      this.helperText,
      this.map,
      this.mapKey})
      : super(key: key);

  @override
  _PassFieldState createState() => _PassFieldState();
}

class _PassFieldState extends State<PassField> {
  bool _obscureText = true;

  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: (value) {
        widget.map[widget.mapKey] = value;
      },
      key: widget.fieldKey,
      obscureText: _obscureText,
      style: TextStyle(fontSize: 16, color: Const.black),
      decoration: InputDecoration(
        contentPadding: const EdgeInsets.fromLTRB(16, 10, 0, 10),
        hintText: '请输入密码',
        hintStyle: TextStyle(color: Const.gray, fontSize: 16),
        filled: true,
        fillColor: Colors.white,
        focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(44),
            ),
            borderSide: BorderSide(
              color: Colors.transparent,
              width: 0,
            )),
        enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(44),
            ),
            borderSide: BorderSide(
              color: Colors.transparent,
              width: 0,
            )),
        suffixIcon: GestureDetector(
          onTap: () {
            setState(() {
              _obscureText = !_obscureText;
            });
          },
          child: Image.asset(
            _obscureText
                ? 'images/login_btn_hide.png'
                : 'images/login_btn_show.png',
            scale: 3,
          ),
        ),
      ),
    );
  }
}
